<?php
/**
 *
 * @package avp
 * @version $Id: $
 * @copyright (c) 2006 Jan-Willem van Ganswijk, Stefan van Essen
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

$root = '../';

define( 'IN_SCRIPT', 'TRUE' ); //OH NO GEEN HACKERS

include( $root . 'includes/common.inc.php' );
include( $root . 'includes/session.class.php' );
include( $root . 'includes/images.inc.php' );

$session = new sessionWrapper();
if( !$session->check_login() ) {
    die();
}

$imageid = ( int )addslashes( $_GET['imageid'] );

$no_resize = false;
if( isset( $_GET['imgsize'] ) && $_GET['imgsize'] ) {
    if( $_GET['imgsize'] == 'max' ) {
        $no_resize = true;
    } else {
        $thumbnail_size = ( int )addslashes( $_GET['imgsize'] );
    }
} else {
    $thumbnail_size = $CONFIG['thumbnail_size'];
}
if( isset( $_GET['pluginid'] ) ) {
    $pluginid = ( int )addslashes( $_GET['pluginid'] );

    $plugindata = $db->query_first( "SELECT * FROM `" . $db->get_table( 'plugins' ) . "` WHERE `pluginid`='{$pluginid}' LIMIT 1" );
    if( is_readable( $root . 'plugins/' . $plugindata['plugindir'] . '/' . $plugindata['imagescript'] ) ) {
        require( $root . 'plugins/' . $plugindata['plugindir'] . '/' . $plugindata['imagescript'] );
        $thumb = get_thumbnail( $no_resize, $thumbnail_size, $plugindata['arguments'] );
    } else {
        die( 'Plugin could not be found' );
    }
} else {
    $row = $db->query_first( "SELECT * FROM `" . $db->get_table( "images" ) . "` WHERE `imageid`='{$imageid}'" );

    $filename = "image-" . $row["imageid"] . "." . $row["ext"];

    list( $origwidth, $origheight ) = getimagesize( $root . "images/" . $filename );
    $source = getsource( $filename );

	if($thumbnail_size > $origheight)
		$no_resize = true;
    if( $no_resize == true ) {
        $thumbwidth = $origwidth;
        $thumbheight = $origheight;
    } else {
        if ( $origwidth > $origheight ) {
            $thumbwidth = $thumbnail_size;
            $thumbheight = ( ( $origheight / $origwidth ) * $thumbnail_size );
        } elseif ( $origwidth < $origheight ) {
            $thumbwidth = ( ( $origwidth / $origheight ) * $thumbnail_size );
            $thumbheight = $thumbnail_size;
        } elseif ( $origwidth == $origheight ) {
            $thumbwidth = $thumbnail_size;
            $thumbheight = $thumbnail_size;
        }
    }

    $thumb = imagecreatetruecolor( $thumbwidth, $thumbheight );
    imagecopyresampled( $thumb, $source, 0, 0, 0, 0, $thumbwidth, $thumbheight, $origwidth, $origheight );
}

switch( $row['ext'] ) {
    case 'jpg':
    case 'jpeg':
        header( 'Content-type: ' . image_type_to_mime_type( IMAGETYPE_JPEG ) );
        imagejpeg( $thumb, '',95 );
        break;
    case 'gif':
        header( 'Content-type: ' . image_type_to_mime_type( IMAGETYPE_GIF ) );
        imagegif( $thumb );
        break;
    case 'png':
    default:
        header( 'Content-type: ' . image_type_to_mime_type( IMAGETYPE_PNG ) );
        imagepng( $thumb );
        break;
}

imagedestroy( $thumb );
imagedestroy( $source );

?>